package gigapaint;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.List;

import javax.swing.ButtonGroup;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JColorChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButtonMenuItem;

@SuppressWarnings("serial")
public class MainFrame extends JFrame implements ActionListener
{
	// JPanel
	JPanel jPanel;
	PaintPanel pPanel = new PaintPanel();

	// Menubar
	JMenuBar menuBar = new JMenuBar();
	JMenu fileMenu = new JMenu("File");
	JMenuItem openItem = new JMenuItem("Open");
	JMenuItem saveItem = new JMenuItem("Save");
	JMenuItem newItem = new JMenuItem("New");
	JMenuItem exitItem = new JMenuItem("Exit");

	JMenu settingsMenu = new JMenu("Settings");
	JMenuItem paintingColourItem = new JMenuItem("Painting Colour");
	JMenuItem fillingColourItem = new JMenuItem("Filling Colour");
	JCheckBoxMenuItem cbFillingModeItem = new JCheckBoxMenuItem("Fill");
	
	JMenu paintingSizeMenu = new JMenu("Size");
	JRadioButtonMenuItem rbSize1 = new JRadioButtonMenuItem("1");
	JRadioButtonMenuItem rbSize5 = new JRadioButtonMenuItem("5");
	JRadioButtonMenuItem rbSize10 = new JRadioButtonMenuItem("10");
	ButtonGroup bgSizes = new ButtonGroup();

	JMenu paintMenu = new JMenu("Paint");
	JRadioButtonMenuItem rbPointMenuItem = new JRadioButtonMenuItem("Point");
	JRadioButtonMenuItem rbLineMenuItem = new JRadioButtonMenuItem("Line");
	JRadioButtonMenuItem rbRectMenuItem = new JRadioButtonMenuItem("Rectangle");
	JRadioButtonMenuItem rbCircleMenuItem = new JRadioButtonMenuItem("Circle");
	JRadioButtonMenuItem rbPolygonMenuItem = new JRadioButtonMenuItem("Polygon");
	JRadioButtonMenuItem rbFillPolygonMenuItem = new JRadioButtonMenuItem("Fill Polygon");
	ButtonGroup bgDrawables = new ButtonGroup();

	public MainFrame()
	{
		jPanel = (JPanel) this.getContentPane();
		jPanel.setLayout(new BorderLayout());
		jPanel.add(pPanel, BorderLayout.CENTER);
		initMenu();
		initWindow();

	}

	private void initMenu()
	{
		fileMenu.add(newItem);
		fileMenu.add(openItem);
		fileMenu.add(saveItem);
		fileMenu.add(exitItem);
		menuBar.add(fileMenu);

		settingsMenu.add(fillingColourItem);
		settingsMenu.add(paintingColourItem);
		settingsMenu.addSeparator();
		settingsMenu.add(cbFillingModeItem);
		menuBar.add(settingsMenu);
		
		bgSizes.add(rbSize1);
		bgSizes.add(rbSize5);
		bgSizes.add(rbSize10);
		paintingSizeMenu.add(rbSize1);
		paintingSizeMenu.add(rbSize5);
		paintingSizeMenu.add(rbSize10);
		menuBar.add(paintingSizeMenu);

		bgDrawables.add(rbPointMenuItem);
		paintMenu.add(rbPointMenuItem);
		bgDrawables.add(rbLineMenuItem);
		paintMenu.add(rbLineMenuItem);
		bgDrawables.add(rbCircleMenuItem);
		paintMenu.add(rbCircleMenuItem);
		bgDrawables.add(rbRectMenuItem);
		paintMenu.add(rbRectMenuItem);
		bgDrawables.add(rbPolygonMenuItem);
		paintMenu.add(rbPolygonMenuItem);
		bgDrawables.add(rbFillPolygonMenuItem);
		paintMenu.add(rbFillPolygonMenuItem);
		menuBar.add(paintMenu);
		

		this.setJMenuBar(menuBar);
	}

	private void initWindow()
	{
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setTitle("GigaPaint");
		this.setSize(500, 500);
		this.setLocationRelativeTo(null);
		this.setVisible(true);

		openItem.addActionListener(this);
		saveItem.addActionListener(this);
		newItem.addActionListener(this);
		exitItem.addActionListener(this);
		paintingColourItem.addActionListener(this);
		fillingColourItem.addActionListener(this);
		cbFillingModeItem.addActionListener(this);
		rbLineMenuItem.addActionListener(this);
		rbPointMenuItem.addActionListener(this);
		rbCircleMenuItem.addActionListener(this);
		rbRectMenuItem.addActionListener(this);
		rbPolygonMenuItem.addActionListener(this);
		rbFillPolygonMenuItem.addActionListener(this);
		rbSize1.addActionListener(this);
		rbSize5.addActionListener(this);
		rbSize10.addActionListener(this);
		
		pPanel.setBackground(Color.WHITE);
		rbPointMenuItem.setSelected(true);
		rbSize1.setSelected(true);

	}

	@Override
	public void actionPerformed(ActionEvent e)
	{
		if (e.getSource() == newItem)
		{
			pPanel.clearList();

		} else if (e.getSource() == openItem)
		{
			this.load();
		} else if (e.getSource() == saveItem)
		{
			this.save(pPanel.getList());
		} else if (e.getSource() == exitItem)
		{
			System.exit(EXIT_ON_CLOSE);
		} else if (e.getSource() == rbPointMenuItem)
		{
			pPanel.setPmode(Modes.point);
		} else if (e.getSource() == rbLineMenuItem)
		{
			pPanel.setPmode(Modes.line);
		} else if (e.getSource() == rbRectMenuItem)
		{
			pPanel.setPmode(Modes.rect);
		} else if (e.getSource() == rbCircleMenuItem)
		{
			pPanel.setPmode(Modes.circle);
		}else if(e.getSource() == rbPolygonMenuItem)
		{
			pPanel.setPmode(Modes.polygon);
		}else if (e.getSource() == rbFillPolygonMenuItem)
		{
			pPanel.setPmode(Modes.fill);
		} else if (e.getSource() == cbFillingModeItem)
		{
			pPanel.setFillMode(cbFillingModeItem.getState());
		} else if (e.getSource() == paintingColourItem)
		{
			pPanel.setPcolor(JColorChooser.showDialog(this, "Bitte Stiftfarbe wählen", pPanel.getPcolor()));
		} else if (e.getSource() == fillingColourItem)
		{
			pPanel.setFcolor(JColorChooser.showDialog(this, "Bitte Füllfarbe wählen", pPanel.getFcolor()));
		} else if (e.getSource() == rbSize1)
		{
			pPanel.setDrawSize(1);
		}else if (e.getSource() == rbSize5)
		{
			pPanel.setDrawSize(5);
		}else if (e.getSource() == rbSize10)
		{
			pPanel.setDrawSize(10);
		}
	}

	private void save(List<Paintable> list)
	{
		try
		{
			ObjectOutputStream output = new ObjectOutputStream(
					new FileOutputStream("list.dat"));
			output.writeObject(list);
			output.close();
		} catch (FileNotFoundException e)
		{
			e.printStackTrace();
		} catch (IOException e)
		{
			e.printStackTrace();
		}

	}

	@SuppressWarnings("unchecked")
	private void load()
	{
		try
		{
			ObjectInputStream input = new ObjectInputStream(
					new FileInputStream("list.dat"));
			pPanel.setList((List<Paintable>) input.readObject());
			input.close();

		} catch (FileNotFoundException e)
		{
			JOptionPane.showMessageDialog(this,
					"Datei nicht gefunden, Programm wird beendet");
			System.exit(ABORT);
			e.printStackTrace();
		} catch (IOException e)
		{
			e.printStackTrace();
		} catch (ClassNotFoundException e)
		{
			e.printStackTrace();
		}
	}

}
